var wxpay = require('../../utils/pay.js')
var app = getApp()
var md5 = require('../../utils/md5.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: undefined,
    mone:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let recharge_amount_min = app.globalData.recharge_amount_min;
    if (!recharge_amount_min) {
      recharge_amount_min = 0;
    }
    this.setData({
      uid: wx.getStorageSync('uid'),
      recharge_amount_min: recharge_amount_min
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindCancel: function () {
    wx.navigateBack({})
  },
  bindSave: function (e) {
    var that = this;
    var amount = e.detail.value.amount;
    if (amount == "" || amount * 1 < 0) {
      wx.showModal({
        title: '错误',
        content: '请填写正确的充值金额',
        showCancel: false
      })
      return
    }
    if (amount * 1 < that.data.recharge_amount_min * 1) {
      wx.showModal({
        title: '错误',
        content: '单次充值金额至少' + that.data.recharge_amount_min + '元',
        showCancel: false
      })
      return
    }
    var service = 'User.RechargeAccount'
    var user_id = wx.getStorageSync('user_id');
    var open_id = wx.getStorageSync('openid');
    var money = amount
    var sign = md5.md5(money + open_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        money: money,
      },
      success: function (res) {
        //console.log(res.data.data.info)
        if (res.data.data.code == 1) {
          wx.showModal({
            loadmore: false
          })
        } else {
          that.setData({
            order_num : res.data.data.info[0].order_num,
            order_id : res.data.data.info[0].order_id,
            money: money
          });
          that.pay();
          //wxpay.wxpay(app, money, order_num, "/pages/ucenter/index");
        }
      }
    }) 
  },

  //发起支付
  pay:function(){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    var openid = wx.getStorageSync('openid');
    var order_id = that.data.order_id;
    var order_num = that.data.order_num;
    var cost = that.data.money;
    var type = 'wechat';
    var client = 'wxa';
    var service = 'Pay.Index';
    var sign = md5.md5(client + cost + openid + order_id + order_num + service + type + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        client: client,
        cost: cost,
        openid: openid,
        order_id: order_id,
        order_num: order_num,
        type: type,
        user_id: user_id,
        sign: sign,
      },
      success: function (res) {
        var result = res.data.data;
        if (result.code != 0) {
          wx.showModal({
            title: '失败',
            content: result.msg,
            showCancel: false
          })
          return;
        }
        wxpay.wxpay(app, cost, order_num, "/pages/my_wallet/index");
      },
      complete: function (res) {
        //console.log(res);
      },
    })
  }
})